package iammry.com.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import java.util.Calendar;

import byl.datepicker.wheelview.OnWheelScrollListener;
import byl.datepicker.wheelview.WheelView;
import byl.datepicker.wheelview.adapter.NumericWheelAdapter;
import iammry.com.R;

public class DataWidget {
	
	private WheelView year;
	private WheelView month;
	private WheelView day;
	private WheelView time;
	private int mYear = 1996;
	private int mMonth = 0;
	private int mDay = 1;
	private int n_year;
	private int n_month;
	private int n_day;
	private LayoutInflater inflater = null;
	public Context context;


	public View getDataPick(Context context)
	
	{
		//this.timeCallBack=timeCallBack;
		this.context=context;
		
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		Calendar c = Calendar.getInstance();
		int norYear = c.get(Calendar.YEAR);
//		int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
//		int curDate = c.get(Calendar.DATE);
		
		int curYear = mYear;
		int curMonth =mMonth+1;
		int curDate = mDay;
		
		View view = inflater.inflate(R.layout.wheel_date_picker, null);
		
		year = (WheelView) view.findViewById(R.id.year);
		
		NumericWheelAdapter numericWheelAdapter1=new NumericWheelAdapter(context,1950, norYear); 
		numericWheelAdapter1.setLabel("年");
		numericWheelAdapter1.setTextColor(R.color.black);
		year.setViewAdapter(numericWheelAdapter1);
		year.setCyclic(true);//是否可循环滑动
		year.addScrollingListener(scrollListener);
		month = (WheelView) view.findViewById(R.id.month);
		
		NumericWheelAdapter numericWheelAdapter2=new NumericWheelAdapter(context,1, 12, "%02d"); 
		numericWheelAdapter2.setLabel("月");
		numericWheelAdapter2.setTextColor(R.color.black);
		month.setViewAdapter(numericWheelAdapter2);
		month.setCyclic(true);
		month.addScrollingListener(scrollListener);
		
		day = (WheelView) view.findViewById(R.id.day);
		initDay(curYear,curMonth);
		day.setCyclic(true);
		
		year.setVisibleItems(5);//设置显示行数
		month.setVisibleItems(5);
		day.setVisibleItems(5);
		
		year.setCurrentItem(curYear - 2000);
		month.setCurrentItem(curMonth - 1);
		day.setCurrentItem(curDate - 1);

		return view;
	}

	
	/**
	 */
	private void initDay(int arg1, int arg2)
	{
		NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(context,1, CalculateDayofMonth.getDay(arg1, arg2), "%02d");
		numericWheelAdapter.setLabel("日");
		numericWheelAdapter.setTextColor(R.color.black);
		day.setViewAdapter(numericWheelAdapter);
	}

	public String getData()
	{

		int n_year = year.getCurrentItem() + 1950;// 年
		int n_month = month.getCurrentItem() + 1;// 月
		initDay(n_year,n_month);
		int n_day = day.getCurrentItem() + 1;
		return  n_year+"-"+n_month+"-"+n_day;

	}


	OnWheelScrollListener scrollListener = new OnWheelScrollListener()
	{
		@Override
		public void onScrollingStarted(WheelView wheel)
		{

		}

		@Override
		public void onScrollingFinished(WheelView wheel)
		{
			int n_year = year.getCurrentItem() + 1950;// 年
			int n_month = month.getCurrentItem() + 1;// 月
			initDay(n_year,n_month);
			int n_day = day.getCurrentItem() + 1;

			
		}
	};
}
